package object3D;

import context.*;

import geometry3D.Point3D;
import util.Util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.media.j3d.Group;
import javax.vecmath.Color3f;



// ***************************************************************************
// *** CLASS: Vindu
// ***************************************************************************
public class Vindu extends Block implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG

  int nRam;
  boolean omramming;

  public Vindu(String ident,Point3D p0,int nRam,boolean omramming,double width,double height,double depth)
  { super(ident,p0,width,height,depth);//,tanTakVinkel,timber);
//    nRam=2; // XXX: !!!
    this.nRam=nRam;
//  omramming=false; // XXX: !!!!!
    if(height>1.0) omramming=true; // XXX: !!!!!
    this.omramming=omramming;
  }
    
  
  // ***************************************************************************
  // *** METHOD: getModel3D
  // ***************************************************************************
  public /*abstract*/ Group getModel3D(Color3f c)
  {
    //if(DEBUG) Debug.println("Vindu-->3D  p0="+p0+", width="+width+", height="+height+", depth="+depth);
    Group tg=new Group();
    addVinduModel(tg,c);
    return(tg);
  }  

  private void addVinduModel
//  (Graphics2D g,Rectangle2D.Double r0,double scale,Color c)
  (Group tg,Color3f c)
  { Color vindusRammeFarge=uthusFarge;
    c=new Color3f(vindusRammeFarge);

//    tg.addChild(super.getModel3D(c));  // TEST !!!!

  }

  
  public void drawProjection(int direction,Graphics2D g,Point2D.Double origo2D,double scale,Color c)
  { Rectangle2D.Double r=projection(direction,origo2D,scale);
    switch(direction)
    {
      case NORTH: tegnVinduFasade(g,r,scale,c); break;
      case SOUTH: tegnVinduFasade(g,r,scale,c); break;
      case EAST: tegnVinduFasade(g,r,scale,c); break;
      case WEST: tegnVinduFasade(g,r,scale,c); break;
//      case ABOVE: drawProjectionAbove(g,r,scale,c); break;
//      case BELOW: drawProjectionBelow(g,r,scale,c); break;
//      case VCUT: drawVerticalCut(g,r,scale,c); break;
      case HCUT: drawHorisontalCut(g,r,scale,c); break;
      default: super.drawProjection(direction,g,origo2D,scale,c);
    }  
  }
      
  private void drawHorisontalCut(Graphics2D g,Rectangle2D.Double r,double scale,Color c)
  { double vinduTykkelse=0.05*scale;
    Util.fill(g,r,Color.white);
    if(r.width>r.height)
    { Util.fillRect(g,r.x,r.y-2,r.width,r.height+4,Color.white);
//    Util.drawRect(g,r.x,r.y+r.height/4,r.width,r.height/2);
      Util.drawRect(g,r.x,r.y+r.height/2-vinduTykkelse/2,r.width,vinduTykkelse);
      if(TEGN_VINDU_DIM)
      { String str=""+(int)(this.width*10)+"/"+(int)(this.height*10);
        Util.label(g,str,r.x+r.width/2,r.y+r.height/3);
      }
    }
    else
    { Util.fillRect(g,r.x-2,r.y,r.width+4,r.height,Color.white);
//    Util.drawRect(g,r.x+r.width/4,r.y,r.width/2,r.height);
      Util.drawRect(g,r.x+r.width/2-vinduTykkelse/2,r.y,vinduTykkelse,r.height);
      if(TEGN_VINDU_DIM)
      { String str=""+(int)(this.depth*10)+"/"+(int)(this.height*10);
        Util.label(g,str,r.x+r.width/2,r.y+r.height/2);
      }
    }
  }

  private void tegnVinduFasade(Graphics2D g,Rectangle2D.Double r0,double scale,Color c)
  {
    Color vindusRammeFarge=Color.white;
   
    // Tegn Vindusramme
    Util.fill(g,r0,vindusRammeFarge);
    
    // Tegn glasset
    double innskudd=0.08*scale; //0.07*scale;
    Rectangle2D.Double r=new Rectangle2D.Double
       (r0.x+innskudd,r0.y+innskudd,r0.width-2*innskudd,r0.height-2*innskudd);
//    double x=r.x;
//    double y=r.y;
//    double width=r.width;
//    double height=r.height;
    Util.fill(g,r,Util.glassFarge);
    Util.draw(g,r);
    double corr=0.01*scale;
    Rectangle2D.Double r1=new Rectangle2D.Double(r.x+corr,r.y+corr,r.width-2*corr,r.height-2*corr);
    Util.draw(g,r1);

    // Tegn evt. sprosse
    int nFag=this.nRam;
    if(nFag>1)
    { // Tegn horisontal sprosse
      double sprossHeight=0.02*scale;
      double y1=r.y+r.height/2; // XXX: Ad'hoc
      Util.fillRect(g,r.x,y1-sprossHeight,r.width,2*sprossHeight,vindusRammeFarge);
      Util.drawRect(g,r.x,y1-sprossHeight,r.width,2*sprossHeight);
      
      // Tegn vertikale sprosser
      for(int i=1;i<nFag;i++)
      { double sprossWidth=0.04*scale;
        double x=r.x+r.width*i/nFag;
        Util.fillRect(g,x-sprossWidth,r.y,2*sprossWidth,r.height,vindusRammeFarge);
        Util.drawRect(g,x-sprossWidth,r.y,2*sprossWidth,r.height);
      }
    }

    // Tegn omramming
    if(omramming)
    { double x=r0.x;
      double y=r0.y;
      double width=r0.width;
      double height=r0.height;
      double ramme=0.05*scale;//8;
      double utskudd=0.05*scale;//5;
      Util.fillRect(g,x-(ramme+utskudd),y-ramme,width+2*(ramme+utskudd),ramme,omrammingFarge);
      Util.drawRect(g,x-(ramme+utskudd),y-ramme,width+2*(ramme+utskudd),ramme);
   
      Util.fillRect(g,x-ramme,y,ramme,height,omrammingFarge);
      Util.drawRect(g,x-ramme,y,ramme,height);
    
      Util.fillRect(g,x+width,y,ramme,height,omrammingFarge);
      Util.drawRect(g,x+width,y,ramme,height);
    
      Util.fillRect(g,x-(ramme+utskudd),y+height,width+2*(ramme+utskudd),ramme,omrammingFarge);
      Util.drawRect(g,x-(ramme+utskudd),y+height,width+2*(ramme+utskudd),ramme);
    } else Util.draw(g,r0);

  }

}
